package com.yige.service.mongoService

import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import com.yige.common.SystemConstant
import com.yige.service.LessonService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * Created by sunhao on 2015/9/17 0017.
 */

@Service
class MongoLessonService extends BasicMongoServiceImp implements LessonService {

    @Autowired
    MongoLessonStuService stuLessonService

    DBCollection getCollection(){
        return mongoTemplate.getCollection(SystemConstant.MONGODB_COL_LESSON)
    }

    @Override
    boolean bindLesson(Object lessonId, Object _id) {
        def lesson = findOneById(lessonId)
        def stuLesson = stuLessonService.findOneById(_id)
        def list = new ArrayList()
        lesson.lessons.each {
            list.add(it)
        }
        list.add( [id:_id,name:stuLesson.name,price:stuLesson.price]as BasicDBObject)
        lesson.lessons = list
        updateOne(lessonId,lesson)
        return true
    }

    @Override
    boolean removeLesson(Object lessonId, Object _id) {
        def lesson = findOneById(lessonId)
        def list = new ArrayList()
        lesson.lessons.each {
            if(!_id.equals(it.id)){
                list.add(it)
            }
        }
        lesson.lessons = list
        updateOne(lessonId,lesson)
        return true
    }
}
